#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	int shmid;
	pid_t pid;
	//创建具有血缘关系的共享内存段
	shmid = shmget (IPC_PRIVATE, 1024, IPC_CREAT | IPC_EXCL | 0600);
	if (-1 == shmid){
		perror("shmget()");
		return -1;
	}
	pid = fork();
	if(pid == -1){
		perror("fork()");
		goto ERROR;
	}
	if(pid == 0){
		void *ptr = shmat (shmid, NULL, 0);
		memcpy (ptr, "can you see me?",15);
		//解除映射
		shmdt (ptr);
		exit (0);
	}
	wait (NULL);
	//将共享内存段映射到父进程的虚拟地址空间中
	
	void *ptr = shmat (shmid, NULL, 0);
	puts (ptr);
	//解除映射
    shmdt (ptr);
	//pause();
	shmctl (shmid, IPC_RMID, ptr);
	return 0;
ERROR:
	shmctl (shmid, IPC_RMID, ptr);  
}
